﻿using System;
using System.Collections.Generic;
using System.Web;
using MySql.Data.MySqlClient;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;




namespace WebApplication3
{
    public class MBDMySql
    {
        MySqlConnection Conexion;
        MySqlCommand Comando;
        MySqlDataReader LectorDatos;
        string Error;

        public MySqlDataReader ResultadoConsulta
        {
            get { return this.LectorDatos; }
        }

        public string MensajeError
        {
            get { return this.Error; }
        }

        public MBDMySql()
        {
            Conexion = new MySqlConnection();
            Conexion.ConnectionString = "Server=localhost;UId=root;Password=123;database=inventario";
        }

        public bool AbrirConexion()
        {
            try
            {
                if (Conexion.State == ConnectionState.Closed)
                {
                    Conexion.Open();
                }
                return true;
            }
            catch (Exception ex)
            {
                Error = ex.Message;
                return false;
            }
        }
        public bool CerrarConexion()
        {
            try
            {
                if (Conexion.State == ConnectionState.Open)
                {
                    Conexion.Close();
                }
                return true;
            }
            catch (Exception ex)
            {
                Error = ex.Message;
                return false;
            }
        }
        // para insert, delete y update

        public bool EjecutarSql(string query)
        {
            try
            {
                Comando = new MySqlCommand();
                Comando.CommandText = query;
                Comando.Connection = Conexion;

                Comando.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                Error = ex.Message;
                return false;
            }
        }
        public bool EjecutarConsulta(string query)
        {
            try
            {
                Comando = new MySqlCommand();
                Comando.CommandText = query;
                Comando.Connection = Conexion;

                LectorDatos = Comando.ExecuteReader();
                return true;
            }
            catch (Exception ex)
            {
                Error = ex.Message;
                return false;
            }




        }

    }
}